package com.hdu.point_offer;


public class lcr192 {
    class Solution {
        public int myAtoi(String str) {
            str = str.trim();
            if (str.isEmpty()) return 0;
            int prev;
            int startIndex;
            if (str.charAt(0) == '-') {
                prev = -1;
                startIndex = 1;
            } else if (str.charAt(0) == '+') {
                prev = 1;
                startIndex = 1;
            } else {
                prev = 1;
                startIndex = 0;
            }
            int ans = 0;
            int limit = Integer.MAX_VALUE / 10;
            for (int i = startIndex; i < str.length(); i++)
            {
                if (!Character.isDigit(str.charAt(i)))
                {
                    return ans * prev;
                }
                else if (ans > limit || (ans == limit && str.charAt(i) - '0' > 7))
                {
                    return prev == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
                }
                else
                {
                    ans = ans * 10 + str.charAt(i) - '0';
                }
            }
            return ans * prev;
        }
    }
}
